<?php
/*
 * Camelot PHP Tools
 * This example illustrates how to download a List Item Attachement
 *
 * @version 2.0
 * @author Bendsoft
 * @package PHP Tools
 * @subpackage Camelot
 * @license FreeBSD License (www.bendsoft.com/licensing/)
 *
 * Note! 
 * Set up settings.php before using this file
 */
error_reporting(E_COMPILE_ERROR);
ini_set('display_errors', '1');


// Load all required files for Camelot PHP Tools
require_once '../loader.php';
?>

<?php include 'base/head.php'; ?>

<h2>Fill out the form to upload an attachment</h2>
<form method="post" enctype="multipart/form-data">
    <div>
        <label>List name:</label> 
        <input type="text" name="listName" value="<?php echo $_POST['listName']; ?>" style="width: 200px;" />
    </div>

    <div>
        <label>List Item ID:</label>
        <input type="text" name="itemId" value="<?php echo $_POST['itemId']; ?>" style="width: 100px;" />
    </div>

    <div>
        <label>File to upload (optional)</label>
        <input type="file" name="file" id="file" />
        <p class="comment">Note! If you don't set the file you must state a file on the server</p>
    </div>

    <div>
        <label>Path to file on server (optional)</label>
        <input type="text" name="filePath" value="<?php echo $_POST['filePath']; ?>" style="width: 400px;" />
        <p class="comment">Set a path to a file on the server if you don't want to upload it by the form</p>
    </div>

    <div>
        <label>Attachment file name (optional):</label>
        <input type="text" name="fileName" value="<?php echo $_POST['fileName']; ?>" style="width: 400px;" /><br />
        <p class="comment">If not set the default name of the file will be used</p>
    </div>

    <div style="padding-top: 5px;">
        <input type="submit">
    </div>
</form>


<?php
if ($_POST) {
    
    // Stopwatch
    $time_pre = microtime(true);    
    
    try {
        // Build command
        $command = new UploadAttachmentCommand();
        $command->connection_name = "SharePointConnection1";
        $command->listName = $_POST['listName'];
        $command->itemId = $_POST['itemId'];
        $command->fileName = $_POST['fileName'];
        $command->data = $_FILES;
        $command->filePath = $_POST['filePath'];
        
        // Execute command
        $result = new SharePointProcedure($command);
        
        
        // If some form of exception occured and SILENT_EXCEPTIONS is set to TRUE
        if (!$result->commandStatus) {
            echo "<p>Silent exceptions is ON, this error message is forced to appear in this example</p>";
            $message = $result->exception->message;
            echo "<h2>Exception: $message</h2>";
            pr($result);
        } else {
            echo "<p>Successfully uploaded the attachment</h2>";
        }
        
    } catch (CamelotException $exc) {
        echo "<h2>Exception: $exc->message</h2>";
        pr($exc);
    }
    
    // ...end stopwatch
    $time_post = microtime(true);
    $exec_time = $time_post - $time_pre;
    echo "<p class=\"comment\">Query executed and data transfered in $exec_time seconds</p>";
}
?>

<?php include 'base/foot.html'; ?>